package io.github.uicdb.et.block;

import io.github.uicdb.et.mixin.IntegerPropertyAccessor;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.common.SpecialPlantable;
import net.neoforged.neoforge.common.util.TriState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.system.linux.Flock;

import java.util.function.Consumer;

public class EarthFarmland extends Block implements SpecialPlantable {
    protected static final VoxelShape SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 15.0, 16.0);

    public EarthFarmland(Properties properties) {
        super(properties);
    }

    @Override
    public boolean canPlacePlantAtPosition(ItemStack itemStack, LevelReader levelReader, BlockPos blockPos, @Nullable Direction direction) {
        return true;
    }

    @Override
    protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
        return false;
    }


    @Override
    protected void randomTick(@NotNull BlockState state, @NotNull ServerLevel level, @NotNull BlockPos pos, @NotNull RandomSource random) {
        super.randomTick(state, level, pos, random);
        BlockPos above = pos.above();
        var up = level.getBlockState(above);
        if (up.getBlock() instanceof BonemealableBlock bonemealableBlock) {
            bonemealableBlock.performBonemeal(level, random, above, up);
        }else if(up.getBlock() instanceof CropBlock cropBlock) {
            cropBlock.growCrops(level,above,up);
        }else if(up.getBlock() instanceof BushBlock) {
            for (int i = 0; i < 3; i++) {
                up.randomTick(level,above,random);
            }
        }

    }

    @Override
    public void spawnPlantAtPosition(ItemStack itemStack, LevelAccessor levelAccessor, BlockPos blockPos, @Nullable Direction direction) {
        if (itemStack.getItem() instanceof BlockItem blockItem && direction == Direction.UP) {
            levelAccessor.setBlock(blockPos.above(), blockItem.getBlock().defaultBlockState(), 3);
        }
    }

    @Override
    public @NotNull TriState canSustainPlant(@NotNull BlockState state, @NotNull BlockGetter level, @NotNull BlockPos soilPosition, @NotNull Direction facing, @NotNull BlockState plant) {
        return TriState.TRUE;
    }

    @Override
    protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
        return SHAPE;
    }
}
